//
//  CMyOrderCenterOrderDetailCheckDelivery.m
//  ProjectforHDX
//
//  Created by motom on 16/8/10.
//  Copyright © 2016年 H.DX. All rights reserved.
//

#import "CMyOrderCenterDetailCheck.h"
#import "DeliverModel.h"
#import "DeliverTrackingModel.h"
#import "DeliverytrackCell.h"
#import "DeliveryTrackHeaderCell.h"

@interface CMyOrderCenterDetailCheck ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UIView *headerView;

@property (nonatomic, strong) DeliverModel *delModel;

@property (nonatomic, strong) NSArray *trackingArr;

@property (nonatomic,assign)CGFloat cellHeight;
@end

@implementation CMyOrderCenterDetailCheck

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupView];
    [self requestData];
}

-(void)setupView
{
    self.labnav.textColor = [UIColor whiteColor];
    self.labnav.text =[THMangerTool language:@"customer.logDetail"];
    
    
    self.tableView = [[UITableView alloc]init];
    [self.tableView setFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.view.height-64)];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UtilCheck hexStringToColor:@"f6f7fb" alpha:1];
    [self.view addSubview:self.tableView];
    
}

-(void)setDeliver:(DeliverModel *)deliver
{
    DeliverModel *model = deliver;
    self.delModel = model;
    [self requestData];
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        DeliveryTrackHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DeliveryTrackHeaderCell"];
        if (!cell) {
            cell = [[DeliveryTrackHeaderCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"DeliveryTrackHeaderCell"];
        }
        [cell  setDeliverModel:self.delModel];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    else     {
        DeliveryTrackCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DeliveryTrackCell"];
        if (!cell) {
            cell = [[DeliveryTrackCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"DeliveryTrackCell"];
        }
        [cell setDeliverModel:self.delModel];
        self.cellHeight = cell.cellHeight;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    return nil;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 85.f;
    }
    if (indexPath.row == 1) {
        return self.cellHeight;
    }
    return  0;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)requestData
{
    
    if (self.delModel.code == nil) {
        [Toolobject showTextWithMessage:[NSString stringWithFormat:@"%@",[THMangerTool language:@"order.no information"]]];
        return;
    }

    [[BDHUD sharedManager] showBDHUD:self.view title:@"HUD.title"];
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    
    params[@"code"] = self.delModel.code;//快递单号
    params[@"com"]=self.delModel.com;//快递公司
    params[@"courierID"] = self.delModel.courierID;
    [[BDHUD sharedManager] showBDHUD:self.view title:nil];
    
    [AFHttpTool postJsonDatawithUrl:@"lookLogistics" parameters:[params mutableCopy] success:^(id responseObject) {
        [[BDHUD sharedManager] dismissHUD];

        if(1==[[[responseObject objectForKey:@"head"] objectForKey:@"status"]  intValue]){
            if([[responseObject objectForKey:@"data"] isKindOfClass:[NSDictionary class]]){
                //数据处理
                NSDictionary *data=[responseObject objectForKey:@"data"];
                [self loadDeliveryData:data];
                [[BDHUD sharedManager] dismissHUD];
                self.tableView.delegate = self;
                self.tableView.dataSource = self;

                [self.tableView reloadData];

            }
            else{
                //返回数据出问题 弹出服务器返回信息
                if([[[responseObject objectForKey:@"head"] allKeys] containsObject:@"info"]&&![UtilCheck DataIsNuLL:[[responseObject objectForKey:@"head"] objectForKey:@"info"]]){
                    [[BDHUD sharedManager] showWords:[[responseObject objectForKey:@"head"] objectForKey:@"info"]];
                    [[BDHUD sharedManager] dismissHUD];

                }
                else{
                    [[BDHUD sharedManager] showWords:[THMangerTool language:@"HUD.nothing"]];
                }
            }
            
        }
        else{
            //返回数据出问题 弹出服务器返回信息
            if([[[responseObject objectForKey:@"head"] allKeys] containsObject:@"info"]&&![UtilCheck DataIsNuLL:[[responseObject objectForKey:@"head"] objectForKey:@"info"]]){
                [[BDHUD sharedManager] showWords:[[responseObject objectForKey:@"head"] objectForKey:@"info"]];
                
            }
            else{
                [[BDHUD sharedManager] showWords:[THMangerTool language:@"HUD.nothing"]];
            }
            
        }
    } fail:^(id responseObject, NSError *error) {
        [[BDHUD sharedManager] dismissHUD];
        if ([[error.userInfo objectForKey:@"NSLocalizedDescription"] isEqualToString:@"请求超时。"]) {
            [[BDHUD sharedManager] showWords:[UtilCheck language:@"order.time.out"]];
        }else {
            [[BDHUD sharedManager] showWords:[UtilCheck language:@"order.network.interruption"]];
        }
        
    }];
    
}

-(void)loadDeliveryData:(NSDictionary *)data
{
    self.delModel = [[DeliverModel alloc]init];
    [self.delModel setValuesForKeysWithDictionary:data];
    [self.delModel setTrackingWithDic:data[@"tracking"]];
    
}



@end
